package com.kuaimacode.kframework.agent.controller.finance;

import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.agent.services.finance.AgentAccountDetailService;
import com.kuaimacode.kframework.agent.util.SessionUtil;
import com.kuaimacode.kframework.common.Constant;
import com.kuaimacode.kframework.common.JsonResult;
import com.kuaimacode.kframework.constants.PageConstants;
import com.kuaimacode.kframework.mybatis.models.finance.AgentAccountDetail;
import com.kuaimacode.kframework.mybatis.models.finance.FinanceQuery;
import com.kuaimacode.kframework.mybatis.models.finance.FinanceTotal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author anzy
 * @date 2019/05/22
 */

/**
 * Controller
 */
@Controller
@RequestMapping({"/agentAccountDetail"})
public class AgentAccountDetailController {
	private static String PAGE_FILE_NAME = "agentAccountDetail/";

	@Autowired
	private AgentAccountDetailService agentAccountDetailServices;


	@RequestMapping(value = {"/index"})
	public String index(@RequestParam(value = "pageNum", required = true, defaultValue = PageConstants.PAGE_NUM+"") int pageNum,
						@RequestParam(value = "pageSize", required = true, defaultValue = PageConstants.PAGE_SIZE+"") int pageSize,
						FinanceQuery query, ModelMap model) {
		PageInfo<AgentAccountDetail> pageInfo = agentAccountDetailServices.findAgentAll(query,pageNum, pageSize);
		// pageInfo.list agentAccountDetail
		//查询平台总交易额、总利润
		FinanceTotal total = agentAccountDetailServices.queryAgentTotal(SessionUtil.getAgentId());
		//交易额合计订单表
		/*Map map = new HashMap<>();
		for(Constant.AgentAccountTranTypeEnum d : Constant.AgentAccountTranTypeEnum.values()){
			map.put(d.getCode(),d.getMessage());

		}
		model.put("tranType", map);*/

		model.put("query",query);
		model.put("total", total);
		model.put("pageInfo", pageInfo);
		return PAGE_FILE_NAME + "index";
	}






	@RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
	public String detail(@PathVariable String id, ModelMap map) {
		AgentAccountDetail agentAccountDetail = agentAccountDetailServices.getById(id);
		map.put("agentAccountDetail", agentAccountDetail);
		return PAGE_FILE_NAME+"detail";
	}


	@ResponseBody
	public JsonResult delete(@PathVariable String id, ModelMap map) {
		try {
			agentAccountDetailServices.removeById(id);
		} catch (Exception e) {
			return JsonResult.failure(e.getMessage());
		}
		return JsonResult.success();
	}


}
